<?php

namespace App\Http\Controllers\Api\V1;

use App\Models\GoldCoin;
use App\Http\Controllers\Api\V1\Queries\GoldCoinQuery;

class GoldCoinsController extends Controller
{
	public function index(GoldCoinQuery $query)
	{
		$user = request()->user();
		$condition = request('condition');
		$query = $query->where('user_id', $user->id);
		$query = !empty(request('start_time')) ? $query->where('created_at', '>', request('start_time')) : $query;
		$query = !empty(request('end_time')) ? $query->where('created_at', '<', request('end_time')) : $query;

		if($condition == 'direct_prize' || $condition == 'team_prize'){
			$arr =GoldCoin::query()->where('created_at', '>', '2022-06-03 00:00:00')
			->where('created_at', '<', '2022-06-03 01:00:00')
			->select('id')->get()->toArray();
			if(!empty($arr)){
				$ids = array_column($arr,'id');
				$query = $query->whereNotIn('id',$ids);
			}
			
		}
		$total = $query->inquire($condition)->sum('money');
		$coins = $query->inquire($condition)->orderByDesc('id')->paginate();
		
		return ['total' => $total, 'data' => $coins];
	}
}
